home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 117 / PC Guia 117.iso / Software / Produtividade / Software2 / Product4 / Setup.exe / drupal-4.6.0 / modules / locale.module < prev    next >
Encoding:
Text File  |  2005-03-31  |  19.4 KB  |  456 lines

  1. <?php
  2. // $Id: locale.module,v 1.120 2005/03/31 09:25:33 unconed Exp $
  3.  
  4. /**
  5.  * @file
  6.  * Enables administrators to manage the site interface languages.
  7.  *
  8.  * When enabled, the site interface can be displayed in different
  9.  * languages. The setup of languages and translations is completely
  10.  * we based. Gettext portable object files are supported.
  11.  */
  12.  
  13. // ---------------------------------------------------------------------------------
  14. // Hook implementations (needed on all page loads)
  15.  
  16. /**
  17.  * Implementation of hook_help().
  18.  */
  19. function locale_help($section = "admin/help#locale") {
  20.   switch ($section) {
  21.     case 'admin/modules#description':
  22.       return t('Enables the translation of the user interface to languages other than English.');
  23.     case 'admin/locale':
  24.     case 'admin/locale/language/overview':
  25.       return t("<p>Drupal allows you to translate the interface to a language other than English. This page provides an overview of the installed languages. You can add more languages on the <a href=\"%add-language\">add language page</a>, or directly by <a href=\"%import\">importing a translation</a>. If there are multiple languages enabled, registered users will be able to set their preference. The site default will be used for users without their own settings, including anonymous visitors.</p><p>There are different approaches to translate the Drupal interface: either by <a href=\"%import\">importing</a> an existing translation, by <a href=\"%search\">translating everything</a> yourself, or by using a combination of these.</p>", array("%search" => url("admin/locale/string/search"), "%import" => url("admin/locale/language/import"), "%add-language" => url("admin/locale/language/add")));
  26.     case 'admin/locale/language/add':
  27.       return t("<p>You need to add all languages you would like to provide the site interface in. If you can't find the desired language in the quick add dropdown, then need to provide the proper language code yourself. The language code might be used to negotiate with browsers and present flags, so it is important to pick one that is standardised for the desired language. You can also add languages by <a href=\"%import\">importing translations</a> directly into a language not yet set up.</p>", array("%import" => url("admin/locale/language/import")));
  28.     case 'admin/locale/language/import':
  29.       return t("<p>This page allows you to import a translation provided in the gettext Portable Object (.po) format.  The easiest way to get your site translated is to grab an existing Drupal translation and to import it.  You can obtain translations from the <a href=\"%url\">Drupal translation page</a>. Note that importing a translation file might take a while.</p>", array('%url' => 'http://drupal.org/project/translations'));
  30.     case 'admin/locale/language/export':
  31.       return t("<p>This page allows you to export Drupal strings. The first option is to export a translation so it can be shared. The second option is to generate a translation template, which contains all Drupal strings, but without their translations. You can use this template to start a new translation using a specialized desktop application.</p>");
  32.     case 'admin/locale/string/search':
  33.       return t("<p>It is often more convenient to get the strings of your setup on the <a href=\"%export\">export page</a>, and start with a desktop Gettext translation editor though. Here you can search in the translated and untranslated strings, and the default English texts provided by Drupal.</p>", array("%export" => url("admin/locale/language/export")));
  34.  
  35.     case 'admin/help#locale':
  36.       return t("
  37.       <p>Most programs are written and documented in English, and primarily use English to interact with users. This is also true for a great deal of web sites. However, most people are less comfortable with English than with their native language, and would prefer to use their mother tongue as much as possible. Many people love to see their web site showing a lot less English, and far more of their own language. Therefore Drupal provides a framework to setup a multi-lingual web site, or to overwrite the default English texts.</p>
  38.       <h3>How to interface translation works</h3>
  39.       <p>Whenever Drupal encounters an interface string which needs to be displayed, it tries to translate it into the currently selected language. If a translation is not available, then the string is remembered, so you can look up untranslated strings easily.</p>
  40.       <p>Drupal provides two options to translate these strings. First is the integrated web interface, where you can search for untranslated strings, and specify their translations via simple web forms. An easier, and much less time consuming method is to import translations already done for your language. This is achieved by the use of GNU gettext Portable Object files. These are editable with quite convenient desktop editors specifically architected for supporting your work with GNU Gettext files. The import feature allows you to add strings from such files into the site database. The export functionality enables you to share your translations with others, generating Portable Object files from your site strings.");
  41.       break;
  42.   }
  43. }
  44.  
  45. /**
  46.  * Implementation of hook_menu().
  47.  */
  48. function locale_menu($may_cache) {
  49.   $items = array();
  50.  
  51.   if ($may_cache) {
  52.     $access = user_access('administer locales');
  53.  
  54.     // Main admin menu item
  55.     $items[] = array('path' => 'admin/locale', 'title' => t('localization'),
  56.       'callback' => 'locale_admin_manage', 'access' => $access);
  57.  
  58.     // Top level tabs
  59.     $items[] = array('path' => 'admin/locale/language', 'title' => t('manage languages'),
  60.       'access' => $access, 'weight' => -10, 'type' => MENU_DEFAULT_LOCAL_TASK);
  61.     $items[] = array('path' => 'admin/locale/string/search', 'title' => t('manage strings'),
  62.       'callback' => 'locale_admin_string', 'access' => $access, 'weight' => 10,
  63.       'type' => MENU_LOCAL_TASK);
  64.  
  65.     // Manage languages subtabs
  66.     $items[] = array('path' => 'admin/locale/language/overview', 'title' => t('list'),
  67.       'callback' => 'locale_admin_manage', 'access' => $access, "weight" => 0,
  68.       'type' => MENU_DEFAULT_LOCAL_TASK);
  69.     $items[] = array('path' => 'admin/locale/language/add', 'title' => t('add language'),
  70.       'callback' => 'locale_admin_manage_add', 'access' => $access, "weight" => 5,
  71.       'type' => MENU_LOCAL_TASK);
  72.     $items[] = array('path' => 'admin/locale/language/import', 'title' => t('import'),
  73.       'callback' => 'locale_admin_import', 'access' => $access, 'weight' => 10,
  74.       'type' => MENU_LOCAL_TASK);
  75.     $items[] = array('path' => 'admin/locale/language/export', 'title' => t('export'),
  76.       'callback' => 'locale_admin_export', 'access' => $access, 'weight' => 20,
  77.       'type' => MENU_LOCAL_TASK);
  78.  
  79.     // Language related callbacks
  80.     $items[] = array('path' => 'admin/locale/language/delete', 'title' => t('confirm'),
  81.       'callback' => 'locale_admin_manage_delete_screen', 'access' => $access,
  82.       'type' => MENU_CALLBACK);
  83.  
  84.     // String related callbacks
  85.     $items[] = array('path' => 'admin/locale/string/edit', 'title' => t('edit'),
  86.       'callback' => 'locale_admin_string', 'access' => $access, 'type' => MENU_CALLBACK);
  87.     $items[] = array('path' => 'admin/locale/string/delete', 'title' => t('delete'),
  88.       'callback' => 'locale_admin_string', 'access' => $access, 'type' => MENU_CALLBACK);
  89.   }
  90.  
  91.   return $items;
  92. }
  93.  
  94. /**
  95.  * Implementation of hook_perm().
  96.  */
  97. function locale_perm() {
  98.   return array('administer locales');
  99. }
  100.  
  101. /**
  102.  * Implementation of hook_user().
  103.  */
  104. function locale_user($type, $edit, &$user, $category = NULL) {
  105.   $languages = locale_supported_languages();
  106.   if ($type == 'form' && $category == 'account' && count($languages['name']) > 1) {
  107.     if ($user->language == '') {
  108.       $user->language = key($languages['name']);
  109.     }
  110.     $languages['name'] = array_map('check_plain', $languages['name']);
  111.     return array(array('title' => t('Interface language settings'), 'data' => form_radios(t("Language"), 'language', $user->language, $languages['name'], t("Selecting a different locale will change the interface language of the site."))));
  112.   }
  113. }
  114.  
  115. // ---------------------------------------------------------------------------------
  116. // Locale core functionality (needed on all page loads)
  117.  
  118. /**
  119.  * Provides interface translation services
  120.  *
  121.  * This function is called from t() to translate a string if needed.
  122.  */
  123. function locale($string) {
  124.   global $locale;
  125.   static $locale_t;
  126.  
  127.   // Store database cached translations in a static var
  128.   if (!isset($locale_t)) {
  129.     $cache = cache_get("locale:$locale");
  130.  
  131.     if ($cache == 0) {
  132.       locale_refresh_cache();
  133.       $cache = cache_get("locale:$locale");
  134.     }
  135.     $locale_t = unserialize($cache->data);
  136.   }
  137.  
  138.   // We have the translation cached (if it is TRUE, then there is no
  139.   // translation, so there is no point in checking the database)
  140.   if (isset($locale_t[$string])) {
  141.     $string = ($locale_t[$string] === TRUE ? $string : $locale_t[$string]);
  142.   }
  143.  
  144.   // We don't have this translation cached, so get it from the DB
  145.   else {
  146.     $result = db_query("SELECT s.lid, t.translation FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid WHERE s.source = '%s' AND t.locale = '%s'", $string, $locale);
  147.     // Translation found
  148.     if ($trans = db_fetch_object($result)) {
  149.       if (!empty($trans->translation)) {
  150.         $locale_t[$string] = $trans->translation;
  151.         $string = $trans->translation;
  152.       }
  153.     }
  154.  
  155.     // Either we have no such source string, or no translation
  156.     else {
  157.       $result = db_query("SELECT lid, source FROM {locales_source} WHERE source = '%s'", $string);
  158.       // We have no such translation
  159.       if ($obj = db_fetch_object($result)) {
  160.         if ($locale) {
  161.           db_query("INSERT INTO {locales_target} (lid, locale) VALUES (%d, '%s')", $obj->lid, $locale);
  162.         }
  163.       }
  164.       // We have no such source string
  165.       else {
  166.         db_query("INSERT INTO {locales_source} (location, source) VALUES ('%s', '%s')", request_uri(), $string);
  167.         if ($locale) {
  168.           $lid = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s'", $string));
  169.           db_query("INSERT INTO {locales_target} (lid, locale) VALUES (%d, '%s')", $lid->lid, $locale);
  170.         }
  171.       }
  172.       // Clear locale cache in DB
  173.       cache_clear_all("locale:$locale");
  174.     }
  175.   }
  176.  
  177.   return $string;
  178. }
  179.  
  180. /**
  181.  * Refreshes database stored cache of translations
  182.  *
  183.  * We only store short strings to improve performance and consume less memory.
  184.  */
  185. function locale_refresh_cache() {
  186.   $languages = locale_supported_languages();
  187.  
  188.   foreach (array_keys($languages['name']) as $locale) {
  189.     $result = db_query("SELECT s.source, t.translation, t.locale FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid WHERE t.locale = '%s' AND LENGTH(s.source) < 75", $locale);
  190.     while ($data = db_fetch_object($result)) {
  191.       $t[$data->source] = (empty($data->translation) ? TRUE : $data->translation);
  192.     }
  193.     cache_set("locale:$locale", serialize($t));
  194.   }
  195. }
  196.  
  197. /**
  198.  * Returns list of languages supported on this site
  199.  *
  200.  * @param $reset Refresh cached language list
  201.  * @param $getall Return all languages (even disabled ones)
  202.  */
  203. function locale_supported_languages($reset = FALSE, $getall = FALSE) {
  204.   static $enabled = NULL;
  205.   static $all = NULL;
  206.  
  207.   if ($reset) {
  208.     unset($enabled); unset($all);
  209.   }
  210.  
  211.   if (is_null($enabled)) {
  212.     $enabled = $all = array();
  213.     $all['name'] = $all['formula'] = $enabled['name'] = $enabled['formula'] = array();
  214.     $result = db_query('SELECT locale, name, formula, enabled FROM {locales_meta} ORDER BY isdefault DESC, enabled DESC, name ASC');
  215.     while ($row = db_fetch_object($result)) {
  216.       $all['name'][$row->locale] = $row->name;
  217.       $all['formula'][$row->locale] = $row->formula;
  218.       if ($row->enabled) {
  219.         $enabled['name'][$row->locale] = $row->name;
  220.         $enabled['formula'][$row->locale] = $row->formula;
  221.       }
  222.     }
  223.   }
  224.   return $getall ? $all : $enabled;
  225. }
  226.  
  227. /**
  228.  * Returns plural form index for a specific number
  229.  *
  230.  * The index is computed from the formula of this language
  231.  */
  232. function locale_get_plural($count) {
  233.   global $locale;
  234.   static $locale_formula, $plurals = array();
  235.  
  236.   if (!isset($plurals[$count])) {
  237.     if (!isset($locale_formula)) {
  238.       $languages = locale_supported_languages();
  239.       $locale_formula = $languages['formula'][$locale];
  240.     }
  241.     if ($locale_formula) {
  242.       $n = $count;
  243.       $plurals[$count] = @eval("return intval($locale_formula);");
  244.       return $plurals[$count];
  245.     }
  246.     else {
  247.       $plurals[$count] = -1;
  248.       return -1;
  249.     }
  250.   }
  251.   return $plurals[$count];
  252. }
  253.  
  254. // ---------------------------------------------------------------------------------
  255. // Language management functionality (administration only)
  256.  
  257. /**
  258.  * Page handler for the language management screen
  259.  */
  260. function locale_admin_manage() {
  261.   include_once 'includes/locale.inc';
  262.   $edit = &$_POST['edit'];
  263.  
  264.   if ($_POST['op'] == t('Save configuration')) {
  265.     // Save changes to existing languages
  266.     $languages = locale_supported_languages(FALSE, TRUE);
  267.     foreach($languages['name'] as $key => $value) {
  268.       if ($edit['sitedefault'] == $key) {
  269.         $edit['enabled'][$key] = 1; // autoenable the default language
  270.       }
  271.       if ($key == 'en') {
  272.         // Disallow name change for English locale
  273.         db_query("UPDATE {locales_meta} SET isdefault = %d, enabled = %d WHERE locale = 'en'", ($edit['sitedefault'] == $key), $edit['enabled'][$key]);
  274.       }
  275.       else {
  276.         db_query("UPDATE {locales_meta} SET name = '%s', isdefault = %d, enabled = %d WHERE locale = '%s'", $edit['name'][$key], ($edit['sitedefault'] == $key), $edit['enabled'][$key], $key);
  277.       }
  278.     }
  279.  
  280.     // Changing the locale settings impacts the interface:
  281.     cache_clear_all();
  282.  
  283.     drupal_goto('admin/locale/language/overview');
  284.   }
  285.  
  286.   print theme('page', _locale_admin_manage_screen());
  287. }
  288.  
  289. /**
  290.  * User interface for the language deletion confirmation screen
  291.  */
  292. function locale_admin_manage_delete_screen() {
  293.   include_once 'includes/locale.inc';
  294.   $langcode = arg(4);
  295.   $edit = $_POST['edit'];
  296.  
  297.   // Check confirmation and if so, delete language
  298.   if ($edit['confirm']) {
  299.     $languages = locale_supported_languages(FALSE, TRUE);
  300.     if (isset($languages['name'][$edit['langcode']])) {
  301.       db_query("DELETE FROM {locales_meta} WHERE locale = '%s'", $edit['langcode']);
  302.       db_query("DELETE FROM {locales_target} WHERE locale = '%s'", $edit['langcode']);
  303.       $message = t('%locale language removed.', array('%locale' => theme('placeholder', t($languages['name'][$edit['langcode']]))));
  304.       drupal_set_message($message);
  305.       watchdog('locale', $message);
  306.     }
  307.  
  308.     // Changing the locale settings impacts the interface:
  309.     cache_clear_all();
  310.     drupal_goto('admin/locale/language/overview');
  311.   }
  312.  
  313.   // Do not allow deletion of English locale
  314.   if ($langcode == 'en') {
  315.     drupal_goto('admin/locale/language/overview');
  316.     return;
  317.   }
  318.  
  319.   // For other locales, warn user that data loss is ahead
  320.   $languages = locale_supported_languages(FALSE, TRUE);
  321.  
  322.   $extra = form_hidden('langcode', $langcode);
  323.   $output = theme('confirm',
  324.                   t('Are you sure you want to delete the language %name?', array('%name' => theme('placeholder', t($languages['name'][$langcode])))),
  325.                   'admin/locale/language/overview',
  326.                   t('Deleting a language will remove all data associated with it. This action cannot be undone.'),
  327.                   t('Delete'),
  328.                   t('Cancel'),
  329.                   $extra);
  330.   print theme('page', $output);
  331. }
  332.  
  333. /**
  334.  * Page handler for the language addition screen
  335.  */
  336. function locale_admin_manage_add() {
  337.   include_once 'includes/locale.inc';
  338.   $edit = &$_POST['edit'];
  339.   $isocodes = _locale_get_iso639_list();
  340.  
  341.   switch ($_POST['op']) {
  342.     // Try to add new language
  343.     case t('Add language'):
  344.       // Check for duplicates
  345.       if (db_num_rows(db_query("SELECT locale FROM {locales_meta} WHERE locale = '%s'", $edit['langcode'])) == 0) {
  346.  
  347.         // Set language name from the available list if needed
  348.         if ($edit['langcode'] && !$edit['langname'] && isset($isocodes[$edit['langcode']])) {
  349.           _locale_add_language($edit['langcode'], $isocodes[$edit['langcode']][0]);
  350.           drupal_goto('admin/locale');
  351.         }
  352.  
  353.         // Add language, if we have the details
  354.         elseif ($edit['langcode'] && $edit['langname']) {
  355.           _locale_add_language($edit['langcode'], $edit['langname']);
  356.           drupal_goto('admin/locale');
  357.         }
  358.  
  359.         // Seems like we have not received some data
  360.         drupal_set_message(t('You need to specify both the language code and the English name of the new language.'), 'error');
  361.       }
  362.       else {
  363.         drupal_set_message(t('The language %language (%code) is already set up.', array('%language' => '<em>'. check_plain($edit['langname']) .'</em>', '%code' => theme('placeholder', $edit['langcode']))), 'error');
  364.       }
  365.       break;
  366.   }
  367.   print theme('page', _locale_admin_manage_add_screen());
  368. }
  369.  
  370. // ---------------------------------------------------------------------------------
  371. // Gettext Portable Object import functionality (administration only)
  372.  
  373. /**
  374.  * Page handler for the translation import screen
  375.  */
  376. function locale_admin_import() {
  377.   include_once 'includes/locale.inc';
  378.   $edit = &$_POST['edit'];
  379.   switch ($_POST['op']) {
  380.     case t('Import'):
  381.  
  382.       // Add language, if not yet supported
  383.       $languages = locale_supported_languages(TRUE, TRUE);
  384.       if (!isset($languages['name'][$edit['langcode']])) {
  385.         $isocodes = _locale_get_iso639_list();
  386.         _locale_add_language($edit['langcode'], $isocodes[$edit['langcode']][0], FALSE);
  387.       }
  388.  
  389.       // Now import strings into the language
  390.       $file = file_check_upload('file');
  391.       if ($ret = _locale_import_po($file, $edit['langcode'], $edit['mode']) == FALSE) {
  392.         $message = t('Translation import of %filename failed.', array('%filename' => theme('placeholder', $file->filename)));
  393.         drupal_set_message($message, 'error');
  394.         watchdog('locale', $message, WATCHDOG_ERROR);
  395.       }
  396.  
  397.       drupal_goto('admin/locale');
  398.       break;
  399.   }
  400.   print theme('page', _locale_admin_import_screen());
  401. }
  402.  
  403.  
  404. // ---------------------------------------------------------------------------------
  405. // Gettext Portable Object export functionality (administration only)
  406.  
  407. /**
  408.  * Page handler for the translation export screen
  409.  */
  410. function locale_admin_export() {
  411.   include_once 'includes/locale.inc';
  412.   switch ($_POST['op']) {
  413.     case t('Export'):
  414.       _locale_export_po($_POST['edit']['langcode']);
  415.       break;
  416.   }
  417.   print theme('page', _locale_admin_export_screen());
  418. }
  419.  
  420.  
  421. // ---------------------------------------------------------------------------------
  422. // String search and editing functionality (administration only)
  423.  
  424. /**
  425.  * Page handler for the string search and administration screen
  426.  */
  427. function locale_admin_string() {
  428.   include_once 'includes/locale.inc';
  429.   $op = ($_POST['op'] ? $_POST['op'] : arg(3));
  430.   $edit =& $_POST['edit'];
  431.  
  432.   switch ($op) {
  433.     case 'delete':
  434.       $output .= _locale_string_delete(db_escape_string(arg(4)));
  435.       $output .= _locale_string_seek();
  436.       break;
  437.     case 'edit':
  438.       $output .= _locale_string_edit(db_escape_string(arg(4)));
  439.       $output .= _locale_string_seek();
  440.       break;
  441.     case t('Search'):
  442.     case 'search':
  443.       $output = _locale_string_seek();
  444.       $output .= _locale_string_seek_form();
  445.       break;
  446.     case t('Save translations'):
  447.       $output .= _locale_string_save(db_escape_string(arg(4)));
  448.       drupal_goto('admin/locale/string/search');
  449.       break;
  450.     default:
  451.   }
  452.   print theme('page', $output);
  453. }
  454.  
  455. ?>
  456.